package com.chixing.service.impl;

import com.chixing.mapper.CustomerMapper;
import com.chixing.mapper.EvaluateMapper;
import com.chixing.mapper.PraisedMapper;
import com.chixing.mapper.ReplyMapper;
import com.chixing.pojo.*;
import com.chixing.service.EvaluateService;
import com.chixing.util.DateUtil;
import com.chixing.util.ServerResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class EvaluateServiceImpl implements EvaluateService {
    @Autowired
    private EvaluateMapper evaluateMapper;

    @Autowired
    private ReplyMapper replyMapper;

    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private PraisedMapper praisedMapper;
     //  根据汽车id  分页查询评论 用户信息 和 评论信息 和 回复者信息及回复的内容
    @Override
    public ServerResponse getEvaluateByCarId(Integer carId,Integer pageNum) {
        EvaluateExample evaluateExample = new EvaluateExample();

        evaluateExample.setOrderByClause("evaluate_time desc");
        evaluateExample.createCriteria().andCarIdEqualTo(carId).andStatusEqualTo(0); // 获得车的评价
        Page<Evaluate> page = PageHelper.startPage(pageNum,5);                // 分页
        List<Evaluate> evaluateList = evaluateMapper.selectByExample(evaluateExample); //获得车的评价
        PageInfo pageInfo = page.toPageInfo();


        List<EvaluateCustomerReplyVO> evaluateCustomerReplyVOList = new ArrayList<>();
        List<ReplyCustomerVO> replyCustomerVOList;
        for(Evaluate evaluate : evaluateList){
            ReplyExample replyExample = new ReplyExample();
            replyExample.createCriteria().andEvaluateIdEqualTo(evaluate.getEvaluateId()).andStatusEqualTo(0);
            List<Reply> replyList = replyMapper.selectByExample(replyExample);      //通过评价id查询所有回复评价的评论
            replyCustomerVOList = new ArrayList<>();          // 用来接收所有回复评价的评论
            Customer customer = customerMapper.selectByPrimaryKey(evaluate.getCustId()); // 查询评价用户
            if(replyList.size()!=0) {
                for (Reply reply : replyList) {
                    Customer replyCustomer = customerMapper.selectByPrimaryKey(reply.getCustId()); //查询所有的回复用户
                    replyCustomerVOList.add(new ReplyCustomerVO(replyCustomer, reply));  // 将所有的回复用户和回复一个VO集合中
                }
            }
            evaluateCustomerReplyVOList.add(new EvaluateCustomerReplyVO(evaluate,customer, replyCustomerVOList)); // 将当前评价用户及评价信息，以及回复者和回复信息封
        }
        pageInfo.setList(evaluateCustomerReplyVOList);
        if(evaluateCustomerReplyVOList.size() != 0)
            return ServerResponse.getSuccess(pageInfo);
        return ServerResponse.getFail(null);
    }
//点赞操作
    @Override
    public ServerResponse updateEvaluateByCarId(Integer evaluateId, Integer custId, Integer status) {
        int row = 0;
        int rows = 0;
        Evaluate evaluate = evaluateMapper.selectByPrimaryKey(evaluateId);
        PraisedExample praisedExample = new PraisedExample();
        praisedExample.createCriteria().andEvaluateIdEqualTo(evaluateId).andCustIdEqualTo(custId);

        List<Praised> praisedList = praisedMapper.selectByExample(praisedExample);
        Praised praised;
        boolean existDataInDb;
        if(praisedList.size()>0) {
            praised = praisedList.get(0);
            praised.setPrisedTime(new Date());
            existDataInDb = true;
        } else {
            praised = new Praised();
            praised.setCustId(custId);
            praised.setEvaluateId(evaluate.getEvaluateId());
            praised.setPrisedTime(new Date());
            existDataInDb = false;
        }

        if(status == 0) {//0为有效
            evaluate.setEvaluatePraisedCount(evaluate.getEvaluatePraisedCount() + 1);
            praised.setStatus(0);
            row =evaluateMapper.updateByPrimaryKey(evaluate);
            if(existDataInDb)
                rows = praisedMapper.updateByPrimaryKey(praised);
            else
                rows = praisedMapper.insert(praised);
        }else if(status==1){
            praised.setStatus(1);
            rows = praisedMapper.updateByPrimaryKey(praised);
            evaluate.setEvaluatePraisedCount(evaluate.getEvaluatePraisedCount() - 1);
            row =evaluateMapper.updateByPrimaryKey(evaluate);

        }
      if(rows>0 && row>0){
          return ServerResponse.updateSuccess(evaluate.getEvaluatePraisedCount());
      }
        return ServerResponse.updateFail(null);
    }

}


